<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>operate</title>
    <link rel="stylesheet" href="../../../static/layer/css/layui.css" media="all"/>
    <link rel="stylesheet" href="../../../static/ucss/contract/contract.css"/>
    <style>
        .layui-form-label {
            width: 120px;
        }

        .layui-input-block {
            padding-left: 40px;
        }

        .smartCode {
            display: none;
        }
    </style>
</head>
<div style="padding: 20px;">
    <form class="layui-form" action="">
        <div class="layui-form-item">
            <label class="layui-form-label">所属商圈/楼宇：</label>
            <div class="layui-input-block room-select">
                <textarea type="text" name="room" readonly placeholder="请选择房源项目"
                          class="layui-input choose-room " style="height: auto"></textarea>
                <div>
                    <div id="room-select-box-container" class="room-select-box-container">
                        <div class="displayflex">
                            <dl class="box-container business-box-container">
                            </dl>
                            <dl class="box-container building-box-container">
                            </dl>
                            <dl class="box-container unit-box-container">
                            </dl>
                            <dl class="box-container floor-box-container">
                            </dl>
                            <dl class="box-container room-box-container">
                            </dl>
                        </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="b-type"></span>编号：</label>
            <div class="layui-input-block">
                <input type="text" name="CODE" lay-verify="required" placeholder="请输入编号" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="b-type"></span>属性：</label>
            <div class="layui-input-block">
                <select name="ATTR" lay-verify="required" lay-filter="attr" class="layui-select">
                    <option value="0">普通表</option>
                    <option value="1">智能表</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item smartCode">
            <label class="layui-form-label">智能表编号：</label>
            <div class="layui-input-block">
                <input type="text" name="SMART_CODE" placeholder="请输入智能表编号" autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label"><span class="b-type"></span>倍率：</label>
            <div class="layui-input-block">
                <input type="number" name="RATES" value="1" lay-verify="required" placeholder="请输入倍率"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">初始读数：</label>
            <div class="layui-input-block">
                <input type="number" name="READ_NUM" value="0" lay-verify="required" placeholder="请输入初始读数"
                       autocomplete="off"
                       class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">抄表日期：</label>
            <div class="layui-input-block">
                <input type="text" name="READ_DATE" lay-verify="required" placeholder="请选择初始读数日期" autocomplete="off"
                       class="layui-input" id="readDate" readonly>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">最大容差：</label>
            <div class="layui-input-block">
                <input type="text" name="TOLERANCE" lay-verify="required" placeholder="总表和子表数据的最大容差" autocomplete="off"
                       class="layui-input" id="TOLERANCE" >
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">备注：</label>
            <div class="layui-input-block">
                <textarea name="REMARKS" placeholder="请输入备注" autocomplete="off" class="layui-textarea"></textarea>
            </div>
        </div>
        <div class="layui-form-item operate-button-group">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit="" lay-filter="save">保存</button>
                <!--<button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
            </div>
        </div>
    </form>
</div>
<script type="text/javascript" src="../../../static/js/common/dateUtils.js"></script>
<script type="text/javascript" src="../../../static/js/common/queryUtils.js"></script>
<script type="text/javascript" src="../../../static/layer/layui.js"></script>
<script>
    var chooseIdArray = new Array(); // 选中的ID数组
    var chooseItemArray = new Array(); // 选中的项目数组
    var chooseOnOff = false;    // 选择房源项目框状态
    var IDS = '';    // 选择的ID以逗号分隔
    layui.use(['jquery', 'form', 'laydate', 'element'], function () {
        var form = layui.form
            , layer = layui.layer
            , element = layui.element
            , laydate = layui.laydate
            , $ = layui.jquery;

        var today = DateUtils.dateFormat(new Date(), "yyyy-MM-dd"); // 今天的日期

        /*------- 下面为房间下拉框相关js -------*/
        $("textarea[name='room']").click(function (e) {
            chooseOnOff = true;
            $(".room-select-box-container").hide();
            $(".room-select-box-container-btn").hide();
            $(this).parent().find(".room-select-box-container").show();
            if (chooseItemArray.length > 0) {
                $(this).parent().find(".room-select-box-container-btn").show();
            }
            e.stopPropagation();
        });

        $(".room-box-container dd").click(function (e) {
            $(".room-select-box-container").hide();
            $(".room-select-box-container-btn").hide();
        });

        $(document).click(function (event) {
            var e = e || window.event; //浏览器兼容性
            var elem = e.target || e.srcElement;
            while (elem) { //循环判断至跟节点，防止点击的是div子元素
                if (elem.id && elem.id == 'room-select-box-container') {
                    return;
                }
                elem = elem.parentNode;
            }
            $(".room-select-box-container").hide();
            $(".room-select-box-container-btn").hide();
            if (chooseOnOff == true) {
                operate.chooseRoom();
                chooseOnOff = false;
            }
        });
        /*------- 上面为房间下拉框相关js -------*/

        //操作类型：add新增，edit修改，view查看
        var operateType = QueryUtils.GetQueryString("type");
        //新增的表类型：b938139a48ed4344a11a7180b48eede6电表，ae5806f8184546208411b99e6117eaa2水表
        var type = QueryUtils.GetQueryString("lx");
        //总分表类型：0总表，1分表
        var btype = QueryUtils.GetQueryString("btype");
        //父ID
        var pid = QueryUtils.GetQueryString("pid");
        //修改的ID
        var id = QueryUtils.GetQueryString("id");

        laydate.render({
            elem: '#readDate'
            , showBottom: false
        });
        $("#readDate").val(today);

        var active = {
            init: function () {
              if (type == 0) {
                  $(".b-type").html("电表");
              }  else if (type == 1) {
                  $(".b-type").html("水表");
              }
            },
            // 初始化商圈楼宇下拉框
            initSelect: function () {
                $.ajax({
                    url: "/business/listBusinessBuilding",
                    data: {},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            if (res.data.length > 0) {
                                $('.buildingGroup').empty();
                                for (var i = 0; i < res.data.length; i++) {
                                    var business = res.data[i];
                                    var str = '<fieldset class="layui-elem-field">\n' +
                                        '          <legend><input type="checkbox" name="PROJECT" value="' + business.ID + '" title="' + business.BUSINESS_NAME + '" lay-skin="primary"></legend>\n' +
                                        '          <div class="layui-field-box">\n';
                                    if (!!business.BUILDING_IDS && !!business.BUILDING_NAMES) {
                                        var str1 = business.BUILDING_IDS.split(',');
                                        var str2 = business.BUILDING_NAMES.split(',');
                                        if (str1.length == str2.length) {
                                            for (var j = 0; j < str1.length; j++) {
                                                str += '<input type="checkbox" name="PROJECT" value="' + str1[j] + '" title="' + str2[j] + '" lay-skin="primary">\n';
                                            }
                                        }
                                    }
                                    str += '          </div>\n' +
                                        '      </fieldset>';
                                    $('.buildingGroup').append(str);
                                }
                                form.render();
                            }
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
            }
        };

        /**
         * 初始化方法集合
         * 使用： init.function()
         * param: obj传this，调用该方法的对象
         *
         */
        var init = {
            // 初始化房间下拉框商圈
            initBusiness: function () {
                $.ajax({
                    url: "/business/list",
                    data: {},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            $(".business-box-container").empty();
                            if (res.data.length > 0) {
                                for (var i = 0; i < res.data.length; i++) {
                                    var chooseFlag = "";
                                    $.each(chooseIdArray, function (index, value) {
                                        if (res.data[i].ID == value) {
                                            chooseFlag = "checked";
                                        }
                                    });
                                    var str = '<dd data-id="' + res.data[i].ID + '" title="' + res.data[i].BUSINESS_NAME + '"><input type="checkbox" name="cb-business" value="' + res.data[i].ID + '" lay-filter="cb-business" lay-skin="primary" ' + chooseFlag + '> ' + res.data[i].BUSINESS_NAME + '</dd>';
                                    $(".business-box-container").append(str);
                                }
                            }
                            form.render();
                            $(".business-box-container dd").click(function (e) {
                                $(this).addClass('layui-this').siblings().removeClass("layui-this");
                                init.initBuilding($(this).data("id"));
                                $(".building-box-container").show();
                                $(".unit-box-container").hide();
                                $(".floor-box-container").hide();
                                $(".room-box-container").hide();
                            });
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
            },
            // 初始化房间下拉框楼宇
            initBuilding: function (businessId) {
                $.ajax({
                    url: "/building/list",
                    data: {BUSINESS_ID: businessId},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            $(".building-box-container").empty();
                            if (res.data.length > 0) {
                                for (var i = 0; i < res.data.length; i++) {
                                    var chooseFlag = "";
                                    $.each(chooseIdArray, function (index, value) {
                                        if (res.data[i].BUILDING_ID == value) {
                                            chooseFlag = "checked";
                                        }
                                    });
                                    var str = '<dd data-id="' + res.data[i].BUILDING_ID + '" title="' + res.data[i].BUILDING_NAME + '"><input type="checkbox" name="cb-building" value="' + res.data[i].BUILDING_ID + '" lay-filter="cb-building" lay-skin="primary" ' + chooseFlag + '> ' + res.data[i].BUILDING_NAME + '</dd>';
                                    $(".building-box-container").append(str);
                                }
                            }
                            form.render();
                            $(".building-box-container dd").click(function (e) {
                                $(this).addClass('layui-this').siblings().removeClass("layui-this");
                                init.initUnit($(this).data("id"));
                                $(".unit-box-container").show();
                                $(".floor-box-container").hide();
                                $(".room-box-container").hide();
                            });
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
            },
            // 初始化房间下拉框单元
            initUnit: function (buildingId) {
                $.ajax({
                    url: "/building/info",
                    data: {BUILDING_ID: buildingId},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            $(".unit-box-container").empty();
                            if (res.unitList.length > 0) {
                                for (var i = 0; i < res.unitList.length; i++) {
                                    var chooseFlag = "";
                                    $.each(chooseIdArray, function (index, value) {
                                        if (res.unitList[i].UNIT_ID == value) {
                                            chooseFlag = "checked";
                                        }
                                    });
                                    var str = '<dd class="dd-unit" data-id="' + res.unitList[i].UNIT_ID + '" title="' + res.unitList[i].UNIT_NAME + ' 单元"><input type="checkbox" name="cb-unit" value="' + res.unitList[i].UNIT_ID + '" lay-filter="cb-unit" lay-skin="primary" ' + chooseFlag + '> ' + res.unitList[i].UNIT_NAME + ' 单元</dd>';
                                    $(".unit-box-container").append(str);
                                }
                            }
                            if (res.itemList.length > 0) {
                                for (var i = 0; i < res.itemList.length; i++) {
                                    var chooseFlag = "";
                                    $.each(chooseIdArray, function (index, value) {
                                        if (res.itemList[i].ROOM_ID == value) {
                                            chooseFlag = "checked";
                                        }
                                    });
                                    var cq = "";
                                    if (res.itemList[i].PROPERTY_RIGHT_TYPE == "0") {
                                        cq = "自有";
                                    } else if (res.itemList[i].PROPERTY_RIGHT_TYPE == "1") {
                                        cq = "非自有";
                                    }
                                    var str = '<dd class="dd-item" data-area="' + res.itemList[i].ROOM_AREA + '" data-id="' + res.itemList[i].ROOM_ID + '" title="【' + res.itemList[i].ROOM_TYPE_NAME + '】' + res.itemList[i].ROOM_NUM + '(' + cq + ')' + '">' +
                                        '<input type="checkbox" name="cb-item" value="' + res.itemList[i].ROOM_ID + '" data-cq="' + res.itemList[i].PROPERTY_RIGHT_TYPE + '" data-name="' + res.itemList[i].ROOM_NUM + '" ' + chooseFlag + ' lay-filter="cb-item" lay-skin="primary">' +
                                        '【' + res.itemList[i].ROOM_TYPE_NAME + '】' + res.itemList[i].ROOM_NUM + '(' + cq + ')' + '</dd>';
                                    $(".unit-box-container").append(str);
                                }
                            }
                            form.render();
                            $(".unit-box-container dd.dd-unit").click(function (e) {
                                $(this).addClass('layui-this').siblings().removeClass("layui-this");
                                $(".floor-box-container").show();
                                $(".room-box-container").hide();
                                init.initFloor($(this).data("id"));
                            });
                            $(".unit-box-container dd.dd-item").click(function (e) {
                                $(this).siblings().removeClass("layui-this");
                                $(".floor-box-container").hide();
                                $(".room-box-container").hide();
                            });
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
            },
            // 初始化房间下拉框楼层
            initFloor: function (unitId) {
                $.ajax({
                    url: "/building/floorInfo",
                    data: {UNIT_ID: unitId},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            $(".floor-box-container").empty();
                            if (res.data.length > 0) {
                                for (var i = res.data.length - 1; i >= 0; i--) {
                                    var chooseFlag = "";
                                    $.each(chooseIdArray, function (index, value) {
                                        if (res.data[i].FLOOR_ID == value) {
                                            chooseFlag = "checked";
                                        }
                                    });
                                    var str = '<dd data-id="' + res.data[i].FLOOR_ID + '" data-bid="' + res.data[i].BUILDING_ID + '" title="' + res.data[i].FLOOR_NAME + ' 层"><input type="checkbox" name="cb-floor" value="' + res.data[i].FLOOR_ID + '" lay-filter="cb-floor" lay-skin="primary" ' + chooseFlag + '> ' + res.data[i].FLOOR_NAME + ' 层</dd>';
                                    $(".floor-box-container").append(str);
                                }
                            }
                            form.render();
                            $(".floor-box-container dd").click(function (e) {
                                $(this).addClass('layui-this').siblings().removeClass("layui-this");
                                init.initRoom($(this).data("bid"), $(this).data("id"));
                                $(".room-box-container").show();
                                e.stopPropagation();
                            });
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
            },
            // 初始化房间下拉框房间
            initRoom: function (buildingId, floorId) {
                $.ajax({
                    url: "/room/list",
                    data: {BUILDING_ID: buildingId, FLOOR_ID: floorId},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "0") {
                            $(".room-box-container").empty();
                            if (res.data.length > 0) {
                                for (var i = 0; i < res.data.length; i++) {
                                    var chooseFlag = "";
                                    $.each(chooseIdArray, function (index, value) {
                                        if (res.data[i].ROOM_ID == value) {
                                            chooseFlag = "checked";
                                        }
                                    });
                                    var cq = "";
                                    if (res.data[i].PROPERTY_RIGHT_TYPE == "0") {
                                        cq = "自有";
                                    } else if (res.data[i].PROPERTY_RIGHT_TYPE == "1") {
                                        cq = "非自有";
                                    }
                                    var str = '<dd data-id="' + res.data[i].ROOM_ID + '" data-wcode="' + res.data[i].WATER_CODE + '" data-ecode="' + res.data[i].ELECTRIC_CODE + '" data-area="' + res.data[i].ROOM_AREA + '" title="' + res.data[i].ROOM_NUM + '(' + cq + ')' + '">' +
                                        '<input type="checkbox" name="cb-room" value="' + res.data[i].ROOM_ID + '" data-name="' + res.data[i].ROOM_NUM + '" lay-filter="cb-room" lay-skin="primary" ' + chooseFlag + '>' +
                                        ' ' + res.data[i].ROOM_NUM + '(' + cq + ')' + '</dd>';
                                    $(".room-box-container").append(str);
                                }
                            }
                            form.render();
                            change.itemSelectChange();
                        } else if (res.errcode === "1") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
            }
        };

        /**
         * 操作方法集合
         * 使用： operate.function()
         * param: obj传this，调用该方法的对象
         *
         */
        var operate = {
            // 选择完房间项目确认
            chooseRoom: function () {
                var str = '';
                IDS = '';
                $.each(chooseItemArray, function (index, value) {
                    if (index == 0) {
                        str += value.name;
                        IDS += value.id;
                    } else {
                        str += "," + value.name;
                        IDS += "," + value.id;
                    }
                });
                $("textarea[name='room']").val(str);
            }
        };

        /**
         * 改变方法集合
         * 使用： change.function()
         * param: obj传this，调用该方法的对象
         *
         */
        var change = {
            // 房间项目下拉选择框复选框
            itemSelectChange: function () {
                if (chooseItemArray.length > 0) {
                    $(".room-select-box-container-btn").show();
                } else {
                    $(".room-select-box-container-btn").hide();
                }
            }
        };

        // active.initSelect();
        active.init();
        init.initBusiness();

        switch (operateType) {
            case "add":
                break;
            case "edit":
                $("input[name='READ_NUM']").attr("disabled", true);
                $("#readDate").attr("disabled", true);
                $.ajax({
                    url: "/hydropowerSetting/info",
                    data: {ID: id},
                    dataType: "json",
                    type: "post",
                    async: false,
                    success: function (res) {
                        if (res.errcode === "00") {
                            IDS = res.data.IDS;
                            chooseIdArray = IDS.split(",");
                            var chooseNameArray = res.data.NAMES.split(",");

                            for (var i = 0; i < chooseIdArray.length; i++) {
                                if (chooseIdArray[i] == "" || typeof(chooseIdArray[i]) == "undefined") {
                                    chooseIdArray.splice(i, 1);
                                    i = i - 1;
                                }
                            }
                            for (var i = 0; i < chooseNameArray.length; i++) {
                                if (chooseNameArray[i] == "" || typeof(chooseNameArray[i]) == "undefined") {
                                    chooseNameArray.splice(i, 1);
                                    i = i - 1;
                                }
                            }

                            $.each(chooseIdArray, function (index, value) {
                                var obj = {};
                                obj.id = value;
                                obj.name = chooseNameArray[index];
                                chooseItemArray.push(obj);
                            });

                            init.initBusiness();
                            operate.chooseRoom();

                            $("input[name='CODE']").val(res.data.CODE);
                            $("select[name='ATTR']").val(res.data.ATTR);
                            if (res.data.ATTR == "0") {
                                $(".smartCode").hide();
                                $("input[name='SMART_CODE']").removeAttr("lay-verify");
                            } else if (res.data.ATTR == "1") {
                                $(".smartCode").show();
                                $("input[name='SMART_CODE']").val(res.data.SMART_CODE);
                                $("input[name='SMART_CODE']").attr("lay-verify", "required");
                            }
                            $("input[name='RATES']").val(res.data.RATES);
                            $("input[name='READ_NUM']").val(res.data.READ_NUM);
                            $("input[name='TOLERANCE']").val(res.data.TOLERANCE);//最大容差
                            $("#readDate").val(res.data.READ_DATE);
                            $("textarea[name='REMARKS']").val(res.data.REMARKS);
                            form.render();
                        } else if (res.errcode === "01") {
                            layer.msg(res.errmsg, function () {
                            });
                        }
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        console.error(XMLHttpRequest.status);
                        console.error(XMLHttpRequest.readyState);
                        console.error(textStatus);
                    }
                });
                break;
        }

        // 商圈复选框
        form.on('checkbox(cb-business)', function (data) {
            var val = data.value;
            var obj = {};
            obj.id = data.value;
            obj.name = data.othis.parent().attr('title');
            if (data.elem.checked == true) {
                chooseIdArray.push(val);
                chooseItemArray.push(obj);
                window.event.stopPropagation();
            } else if (data.elem.checked == false) {
                chooseIdArray = $.grep(chooseIdArray, function (value) {
                    return value != val;
                });
                for (var j = 0; j < chooseItemArray.length; j++) {
                    if (chooseItemArray[j].id == obj.id) {
                        chooseItemArray.splice(j, 1);
                        j--;
                    }
                }
                form.render('checkbox');
                window.event.stopPropagation();
            }
            operate.chooseRoom();
        });
        // 楼宇复选框
        form.on('checkbox(cb-building)', function (data) {
            var val = data.value;
            var obj = {};
            obj.id = data.value;
            obj.name = data.othis.parent().attr('title');
            if (data.elem.checked == true) {
                chooseIdArray.push(val);
                chooseItemArray.push(obj);
                window.event.stopPropagation();
            } else if (data.elem.checked == false) {
                chooseIdArray = $.grep(chooseIdArray, function (value) {
                    return value != val;
                });
                for (var j = 0; j < chooseItemArray.length; j++) {
                    if (chooseItemArray[j].id == obj.id) {
                        chooseItemArray.splice(j, 1);
                        j--;
                    }
                }
                form.render('checkbox');
                window.event.stopPropagation();
            }
            operate.chooseRoom();
        });
        // 单元复选框
        form.on('checkbox(cb-unit)', function (data) {
            var val = data.value;
            var obj = {};
            obj.id = data.value;
            obj.name = data.othis.parent().attr('title');
            if (data.elem.checked == true) {
                chooseIdArray.push(val);
                chooseItemArray.push(obj);
                window.event.stopPropagation();
            } else if (data.elem.checked == false) {
                chooseIdArray = $.grep(chooseIdArray, function (value) {
                    return value != val;
                });
                for (var j = 0; j < chooseItemArray.length; j++) {
                    if (chooseItemArray[j].id == obj.id) {
                        chooseItemArray.splice(j, 1);
                        j--;
                    }
                }
                form.render('checkbox');
                window.event.stopPropagation();
            }
            operate.chooseRoom();
        });
        // 项目复选框
        form.on('checkbox(cb-item)', function (data) {
            var val = data.value;
            var obj = {};
            obj.id = data.value;
            obj.name = data.othis.parent().find("input").data('name');
            if (data.elem.checked == true) {
                chooseIdArray.push(val);
                chooseItemArray.push(obj);
                window.event.stopPropagation();
            } else if (data.elem.checked == false) {
                chooseIdArray = $.grep(chooseIdArray, function (value) {
                    return value != val;
                });
                for (var j = 0; j < chooseItemArray.length; j++) {
                    if (chooseItemArray[j].id == obj.id) {
                        chooseItemArray.splice(j, 1);
                        j--;
                    }
                }
                form.render('checkbox');
                window.event.stopPropagation();
            }
            operate.chooseRoom();
        });
        // 楼层复选框
        form.on('checkbox(cb-floor)', function (data) {
            var val = data.value;
            var obj = {};
            obj.id = data.value;
            obj.name = data.othis.parent().attr('title');
            if (data.elem.checked == true) {
                chooseIdArray.push(val);
                chooseItemArray.push(obj);
                window.event.stopPropagation();
            } else if (data.elem.checked == false) {
                chooseIdArray = $.grep(chooseIdArray, function (value) {
                    return value != val;
                });
                for (var j = 0; j < chooseItemArray.length; j++) {
                    if (chooseItemArray[j].id == obj.id) {
                        chooseItemArray.splice(j, 1);
                        j--;
                    }
                }
                form.render('checkbox');
                window.event.stopPropagation();
            }
            operate.chooseRoom();
        });
        // 房间复选框
        form.on('checkbox(cb-room)', function (data) {
            var val = data.value;
            var obj = {};
            obj.id = data.value;
            obj.name = data.othis.parent().find("input").data('name');
            if (data.elem.checked == true) {
                chooseIdArray.push(val);
                chooseItemArray.push(obj);
                window.event.stopPropagation();
            } else if (data.elem.checked == false) {
                chooseIdArray = $.grep(chooseIdArray, function (value) {
                    return value != val;
                });
                for (var j = 0; j < chooseItemArray.length; j++) {
                    if (chooseItemArray[j].id == obj.id) {
                        chooseItemArray.splice(j, 1);
                        j--;
                    }
                }
                form.render('checkbox');
                window.event.stopPropagation();
            }
            operate.chooseRoom();
        });

        // 表属性下拉框
        form.on('select(attr)', function (data) {
            if (data.value == "0") {
                $(".smartCode").hide();
                $("input[name='SMART_CODE']").removeAttr("lay-verify");
            } else if (data.value == "1") {
                $(".smartCode").show();
                $("input[name='SMART_CODE']").attr("lay-verify", "required");
            }
            form.render();
        });

        // 监听提交
        form.on('submit(save)', function (data) {
            if (!IDS) {
                layer.msg("至少选择一个", {offset: '150px'});
                return false;
            }
            data.field.TYPE = type;
            data.field.BTYPE = btype;
            data.field.IDS = IDS;
            data.field.PID = pid;
            /*layer.alert(JSON.stringify(data.field), {
                title: '最终的提交信息'
            });*/
            if (operateType === "add") {
                layer.msg('确认保存信息吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/hydropowerSetting/save",
                            data: data.field,
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {
                                if (res.errcode === "00") {
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭新建商圈页面
                                    var pActive = parent.layui.data.pActive;
                                    pActive.initTable(type);
                                    parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功
                                } else if (res.errcode === "01") {
                                    layer.msg(res.errmsg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            } else if (operateType === "edit") {
                data.field.ID = id;
                layer.msg('确认保存信息吗', {
                    time: 0
                    , btn: ['确定', '取消']
                    , shade: 0.3
                    , yes: function (index) {
                        layer.close(index); // 关闭当前提示框
                        $.ajax({
                            url: "/hydropowerSetting/edit",
                            data: data.field,
                            dataType: "json",
                            type: "post",
                            async: false,
                            success: function (res) {
                                if (res.errcode === "00") {
                                    var i = parent.layer.getFrameIndex(window.name);
                                    parent.layer.close(i);  // 关闭新建商圈页面
                                    var pActive = parent.layui.data.pActive;
                                    pActive.initTable(type);
                                    parent.layer.msg("保存成功", {offset: '150px'});   // 父页面弹出保存成功
                                } else if (res.errcode === "01") {
                                    layer.msg(res.errmsg, function () {
                                    });
                                }
                            },
                            error: function (XMLHttpRequest, textStatus, errorThrown) {
                                console.error(XMLHttpRequest.status);
                                console.error(XMLHttpRequest.readyState);
                                console.error(textStatus);
                            }
                        });
                    }
                });
            }
            return false;
        });

    });
</script>
</body>
</html>